import config from './config.js';
// console.log('config',config)
// import {status} from'./api/login.js'
// const config = {
// 	baseUrl:'http://120.79.203.248:8012/'
// }
const http = ({
	url = '', //请求地址 string
	param = {}, // 请求参数 obj
	method = 'get', //请求方式 string
	contentType, //请求头 obj
	isPermissions = false, //是否添加token
	isToast=true
} = {}) => {
	const token = uni.getStorageSync('token')
	let header = {
		"content-type": contentType ? contentType : method == 'POST' ? 'application/json' :
			'application/x-www-form-urlencoded',
		'Accept-Language': 'zh-CN'
	}
	if (isPermissions) {
		header['token'] = `mini- ${token}`
	}
	// if(isPermissions){
	// 	header['token']=`mini- eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOiJNSU5JOjIiLCJyblN0ciI6InY5aDdxYzgxSkpLeldrNGJwMm90aWZzT0tzdjl6azBuIiwiVVNFUl9JTkZPIjp7ImlkIjoyLCJuaWNrTmFtZSI6IueUqOaItzE1OSoqKiowMTkyIiwicGhvbmUiOiIxNTk4OTQwMDE5MiIsInVzZXJUeXBlIjoxLCJ1c2VyVHlwZU5hbWUiOiLmma7pgJrnlKjmiLcifX0.grxcIcxOHUTk2EJXVy9qjpJCGeIvFJ0JUlQC26RX1kw`
	// }
	// if(status.wxappkey ){
	// 	header['wxappkey']=status.wxappkey
	// }
	uni.showLoading({
		title: '网络请求中'
	});
	let timeStart = Date.now();;
	return new Promise((resolve, reject) => {

		uni.request({
			url: getUrl(url),
			data: param,
			header: header,
			method: method,
			complete: (res) => {
				uni.hideLoading();
				// console.log(`请求地址:${getUrl(url)}`);
				// console.log(`请求参数:${JSON.stringify(param)}`);
				// console.log(`耗时${Date.now() - timeStart}ms`);
				// console.log(`请求头:${JSON.stringify(header)}`);
				if (res.statusCode >= 200 && res.statusCode < 300) {

						
					if (res.data.code == -500) {
						isMssage(res.data.message)
					}else if (res.data.code == -5014 && isToast) {
						console.log(isToast,url)
						toastLogin()
					}else if(res.data.code == -5015){
						toAuth()
						return false
					}else{
						
					}
					resolve(res)
				} else {
					if (res.statusCode == 500) {
						isMssage('服务器错误，请联系管理员')
					}
					reject(res)
				}
			}
		})
	})
}

const getUrl = (url) => {
	if (url.indexOf('://') == -1) {
		url = config.baseUrl + url;
	}

	console.log(url)
	return url
}

const _get = (url, param = {}, isPermissions) => {
	return http({
		url,
		param,
		isPermissions
	})
}



const _post = (url, param = {}, isPermissions, isToast=true,contentType) => {
	if (!param.token) {
		param.token = '0645376f'
	}
	return http({
		url,
		param,
		method: 'POST',
		isPermissions,
		isToast,
		contentType
	})
}

const _put = (url, param = {}, isPermissions) => {
	return http({
		url,
		param,
		method: 'put',
		isPermissions
	})
}

const _delete = (url, param = {}, isPermissions) => {
	return http({
		url,
		param,
		method: 'delete',
		isPermissions
	})
}


const isMssage = (text, duration = 2000) => {
	uni.showToast({
		title: text,
		icon: 'none',
		duration: duration
	})
}
const toastLogin = () => {
	uni.showModal({
		title: '提示',
		content: '是否前往登录',
		success: function(res) {
			if (res.confirm) {
				console.log('用户点击确定');
				uni.navigateTo({
					url: '/pagesA/login/login'
				})
			} else if (res.cancel) {
				console.log('用户点击取消');
			}
		}
	});
}
const toAuth = (id) =>{
	uni.showModal({
		title: '您还未认证',
		content: '是否前往实名认证',
		success: function(res) {
			if (res.confirm) {
				console.log('用户点击确定');
				uni.navigateTo({
					url: '/pagesA/authentication/authentication?realNameFlag='+id
				})
			} else if (res.cancel) {
				console.log('用户点击取消');
			}
		}
	});
}
export default {
	_get,
	_post,
	_put,
	_delete,
	isMssage,
	toastLogin,
	toAuth
}